<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
</body>
<script>
// 数组的扁平化
let arr = [1,2,[3,4,[5,6,[7],8],9,10]]
console.log( arr );

// 方式一 flat(参数)深度
console.log('way 1', arr.flat(Infinity))

// 方式二 reduce方法
function flat(arr, n) {
    if(n == 0) return arr
    return arr.reduce((cur, pre) => {
        return Array.isArray(pre) ? cur.concat(flat(pre, n - 1)) : cur.concat(pre)
    }, [])
}
console.log('way 2', flat(arr));

// 方式三 数组转成字符串，再将字符串转成数组
function flatFn(arr) {
    return arr.join(',').split(',').map(item => {
        return parseInt(item)
    })
}
console.log('way 3',flatFn(arr));

</script>
</html>